package com.zhan.ielts.task;

import com.zhan.ielts.dao.ParamDao;
import com.zhan.ielts.domain.Param;
import com.zhan.ielts.domain.SpeakingRecall;
import com.zhan.ielts.service.EmailService;
import com.zhan.ielts.service.SpeakingRecallService;
import com.zhan.ielts.utils.Contents;
import com.zhan.ielts.utils.CustomPropertisUtils;
import com.zhan.ielts.utils.SendSMSSelf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class SpeakingRecallJob {

    @Autowired
    SpeakingRecallService speakingRecallService;

    @Autowired
    EmailService emailService;

    @Autowired
    private ParamDao paramDao;

    private Logger log = LoggerFactory.getLogger(SpeakingRecallJob.class);

    @Scheduled(cron = "0 0 8-23 * * ?")
    public void run() {
        String resourcePath = System.getenv("resources_path");
        if ("pro".equals(resourcePath)) {
            log.info("start SpeakingRecallJob:" + new Date());
            SpeakingRecall query = new SpeakingRecall();
            query.setNeedReview(2);
            Integer count = speakingRecallService.selectUntreatedCount(query);
            if (null != count && count > 0) {
                sendSmsAndEmail(count);
            }
            log.info("end SpeakingRecallJob:" + new Date());
        }
    }

    private boolean sendSmsAndEmail(int count) {
        Param param = new Param();
        param.setType(Contents.SMS_TYPE);
        param.setParamName(Contents.SMS_PHONE_PARAM_NAME);
        param = paramDao.selectOne(param);

        boolean flag = false;
        Param paramContent = new Param();
        paramContent.setType(Contents.SMS_TYPE);
        paramContent.setParamName(Contents.SMS_CONTENT_PARAM_NAME);
        paramContent = paramDao.selectOne(paramContent);
        String phoneName = param.getParamValue();
        String[] phoneNames = phoneName.split(",");
        for (int i = 0; i < phoneNames.length; i++) {
            String[] nameAndPhone = phoneNames[i].split(":");
            String content = String.format(paramContent.getParamValue(), nameAndPhone[0], count);
            content =content+nameAndPhone[0];
            flag = SendSMSSelf.sendSMS(nameAndPhone[1], content, Contents.SMS_TOKEN_UNSET);
        }
        Param paramMail = new Param();
        paramMail.setType(Contents.MAIL_TYPE);
        paramMail.setParamName(Contents.MAIL_ADDRESS);
        paramMail = paramDao.selectOne(paramMail);
        String emailName = paramMail.getParamValue();
        String[] emailNames = emailName.split(",");
        for (int i = 0; i < emailNames.length; i++) {
            String[] nameAndEmail = emailNames[i].split(":");
            try {
                String content = String.format(paramContent.getParamValue(), "  ", count);
                content = content+nameAndEmail[0];
                emailService.sendMailCommen("有待审核口语回忆", content, new String[]{nameAndEmail[1]});
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return flag;
    }
}
